import { Column, Entity } from 'typeorm';
import { BasePo } from '@/common/base.po';
import { GenerateUUID } from '@/utils/uuid';

@Entity({
  name: 'user',
  comment: '用户',
})
export class User extends BasePo {
  constructor(phone: string, openId: string, nickName?: string, avatarUrl?: string) {
    super();
    this.name = phone;
    this.openId = openId;
    this.phone = phone;
    this.balance = 0;
    this.token = 0;
    this.invitationCode = GenerateUUID();
    this.status = UserStatus.VISITOR;
    this.community = '';
    this.building = '';
    this.unit = '';
    this.room = '';
    this.fullAddress = '';
    this.nickName = nickName || '';
    this.avatarUrl = avatarUrl || '';
  }

  @Column({
    type: 'varchar',
    length: 20,
    comment: '用户名',
  })
  name: string;

  @Column({
    name: 'open_id',
    type: 'varchar',
    length: 50,
    comment: '微信小程序openId',
  })
  openId: string;

  @Column({
    type: 'varchar',
    length: 16,
    comment: '手机号',
  })
  phone: string;

  @Column({
    type: 'int',
    comment: '余额',
  })
  balance: number;

  @Column({
    type: 'int',
    comment: '剩余积分',
  })
  token: number;

  @Column({
    name: 'invitation_code',
    type: 'varchar',
    length: 32,
    comment: '邀请码',
  })
  invitationCode: string;

  @Column({
    type: 'varchar',
    length: 10,
    comment: '用户状态：游客/会员',
  })
  status: string;

  @Column({
    type: 'varchar',
    length: 31,
    comment: '小区名称',
  })
  community: string;

  @Column({
    type: 'varchar',
    length: 31,
    comment: '楼栋号',
  })
  building: string;

  @Column({
    type: 'varchar',
    length: 31,
    comment: '单元号',
  })
  unit: string;

  @Column({
    type: 'varchar',
    length: 31,
    comment: '门牌号',
  })
  room: string;

  @Column({
    type: 'varchar',
    length: 127,
    comment: '完整地址字符串',
  })
  fullAddress: string;

  @Column({
    name: 'nick_name',
    type: 'varchar',
    length: 50,
    comment: '用户昵称',
    default: '',
  })
  nickName: string;

  @Column({
    name: 'avatar_url',
    type: 'varchar',
    length: 255,
    comment: '用户头像URL',
    default: '',
  })
  avatarUrl: string;
}

export enum UserStatus {
  VISITOR = 'visitor',
  VIP = 'vip',
}
